# -*- coding: utf-8 -*-
# @Time    : 2020/5/12 10:33
# @Author  : suhong
# @File    : save_routers.py
# @Software: PyCharm
import datetime

from fastapi import APIRouter

from app.allsubdb.chaoxingqk.models import CxJournalDetailModel, CxJournalDetail
from app.core.return_info import ReturnInfo, SUCCESS

router = APIRouter()

@router.post("/insert/cxjournaldetail")
def wfjournalpageid(ckjpid: CxJournalDetailModel):
    return_info = ReturnInfo()
    m_result = CxJournalDetail.objects(cid=ckjpid.cid)
    if m_result:  # mongoengine.queryset.queryset.QuerySet
        # qwlen = m_result[0].qwDiv_len
        # if int(ckjpid.qwDiv_len) > int(qwlen):
        m_result.update(update_time=datetime.datetime.now(),
                        qwcontentDivId_stat = ckjpid.qwcontentDivId_stat,
                        pdf_html_stat=ckjpid.pdf_html_stat,
                        old_latest_stat=ckjpid.old_latest_stat,
                        up_hdfs_stat=ckjpid.up_hdfs_stat,
                        qwDiv_len=ckjpid.qwDiv_len,
                        html_detail = ckjpid.html_detail)
        # jwpp.reload()
        return_info.status = SUCCESS
        return_info.data = {"boos": [str(one_obj.id) for one_obj in m_result]}
        return return_info.todict()  #
    else:
        jwpp = CxJournalDetail(cid = ckjpid.cid,
                               down_date = ckjpid.down_date,
                               jsondicts = ckjpid.jsondicts,
                               qwcontentDivId_stat = ckjpid.qwcontentDivId_stat,
                               qwDiv_len = ckjpid.qwDiv_len,
                               pdf_html_stat=ckjpid.pdf_html_stat,
                               old_latest_stat=ckjpid.old_latest_stat,
                               up_hdfs_stat = ckjpid.up_hdfs_stat,
                               html_detail = ckjpid.html_detail,
                               update_time=datetime.datetime.now(),
                               create_time=datetime.datetime.now()
                               )
        jwpp.save()
        return_info.status = SUCCESS
        return_info.data = {"boos": str(jwpp.id)}
        return return_info.todict()


@router.get("/get/cxjournaldetail_up_hdfs_stat_id")
def read_root():
    m_result = CxJournalDetail.objects(up_hdfs_stat='0')
    for r in m_result:
        print(r.cid)
    return ""



@router.get("/get/cxjournaldetail")
def read_root(cid:str):
    m_result = CxJournalDetail.objects(cid=cid)
    for r in m_result:
        print(r.html_detail)
        print(r.cid)
        print(r.jsondicts['url'])
    return ""